package com.wangfugui.apprentice.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangfugui.apprentice.common.util.ResponseUtils;
import com.wangfugui.apprentice.dao.domain.Blog;
import com.wangfugui.apprentice.dao.dto.LoginBo;
import com.wangfugui.apprentice.entity.Menu;
import com.wangfugui.apprentice.entity.Student;
import com.wangfugui.apprentice.service.IBlogService;
import com.wangfugui.apprentice.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * (Student)表控制层
 *
 * @author makejava
 * @since 2025-06-12 10:36:08
 */
@RestController
@RequestMapping("api/student")
@Api(tags = "学生模块")
@CrossOrigin
public class StudentController extends BaseController<StudentService, Student> {
  @Resource
  private StudentService service;
  @Resource
  private StringRedisTemplate template;

  @ApiOperation("登录")
  @PostMapping("/login")
  public ResponseUtils login(@RequestBody Student req) {
    if( req.getName().equals("admin")&&req.getPassword().equals("123456")){
      return ResponseUtils.success("idfanofnaoingoineoangoienwigngeweio");
    }
    if( req.getName().equals("test")&&req.getPassword().equals("test")){
      return ResponseUtils.success("hniunoanfeoWNVofnonwe");
    }
    return ResponseUtils.error("用户名密码不正确，请重新输入");
  }

  @ApiOperation("发送验证码")
  @PostMapping("/login/send")
  public ResponseUtils loginIfo(@RequestBody Student req) {
    QueryWrapper<Student> wrapper = new QueryWrapper<>();
    wrapper.eq("phone", req.getPhone()); // 对应数据库字段 phone = ?

    // 调用 IService 提供的方法
    Student student = service.getOne(wrapper);
    if(student == null){
      return ResponseUtils.error("手机号不存在，请重新输入");
    }
    String code = template.opsForValue().get("code");
    if (code != null) {
      return ResponseUtils.error("手机号已发放验证码");
    }
    template.opsForValue().set(req.getPhone(),"123456",10, TimeUnit.MINUTES);
    return ResponseUtils.success();
  }
  @ApiOperation("验证码登录")
  @PostMapping("/login/info")
  public ResponseUtils loginByPhone(@RequestBody LoginBo req) {
    QueryWrapper<Student> wrapper = new QueryWrapper<>();
    wrapper.eq("phone", req.getPhone()); // 对应数据库字段 phone = ?

    // 调用 IService 提供的方法
    Student student = service.getOne(wrapper);
    if(student == null){
      return ResponseUtils.error("手机号不存在，请重新输入");
    }
    String code = template.opsForValue().get("code");
    if (code ==null) {
      return ResponseUtils.error("手机号未发放验证码");
    }
    template.opsForValue().set("code","123456",10, TimeUnit.MINUTES);
    return ResponseUtils.success();
  }
  @ApiOperation("得到菜单")
  @GetMapping("/get/menu")
  public ResponseUtils getMenu(@RequestParam String name) {
    List<Menu> menus = new ArrayList<>();

    if (name.equals("admin")) {
      // admin 用户拥有完整菜单
      menus.add(new Menu("/home", "home", "首页", "s-home", "Home.vue"));
      menus.add(new Menu("/mall", "mall", "商品管理", "video-play", "Mall.vue"));
      menus.add(new Menu("/user", "user", "用户管理", "user", "User.vue"));

      Menu otherMenu = new Menu();
      otherMenu.setLabel("其他");
      otherMenu.setIcon("location");
      List<Menu> children = new ArrayList<>();
      children.add(new Menu("/page1", "page1", "页面1", "setting", "Page1.vue"));
      children.add(new Menu("/page2", "page2", "页面2", "setting", "Page2.vue"));
      otherMenu.setChildren(children);

      menus.add(otherMenu);
    } else if (name.equals("test")) {
      // test 用户只看得到首页和其他菜单
      menus.add(new Menu("/home", "home  ", "首页", "s-home", "Home.vue"));

      Menu otherMenu = new Menu();
      otherMenu.setLabel("其他");
      otherMenu.setIcon("location");
      List<Menu> children = new ArrayList<>();
      children.add(new Menu("/page1", "page1", "页面1", "setting", "Page1.vue"));
      otherMenu.setChildren(children);

      menus.add(otherMenu);
    } else {
      return ResponseUtils.error("获取菜单失败");
    }

    return ResponseUtils.success(menus);
  }
}

